package com.hotau.util;

import java.lang.reflect.Field;
import java.util.Calendar;

import android.app.DatePickerDialog;
import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.DatePicker;

public class MyDatePickerDialog extends android.app.DatePickerDialog {

	private static final int DEFAULT_START_YEAR = 1900;
	private static final int DEFAULT_END_YEAR = 2100;

	public static final String FULL = "yyyy-MM-dd EEE";

	public MyDatePickerDialog(Context context, int theme,
			OnDateSetListener callBack, int year, int monthOfYear,
			int dayOfMonth) {
		super(context, theme, callBack, year, monthOfYear, dayOfMonth);
		setRange();
		updateTitle(year, monthOfYear, dayOfMonth);
	}

	public MyDatePickerDialog(Context context, OnDateSetListener callBack,
			int year, int monthOfYear, int dayOfMonth) {
		super(context, callBack, year, monthOfYear, dayOfMonth);
		setRange();
		updateTitle(year, monthOfYear, dayOfMonth);

	}

	private void setRange() {
		try {
			Field[] fields = DatePickerDialog.class.getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				if ("mDatePicker".equals(field.getName())) {
					Log.d("mytag", "aaa mDatePickerField.get(this)="
							+ field.get(this).getClass().toString());
				}
			}
			Field mDatePickerField = DatePickerDialog.class
					.getDeclaredField("mDatePicker");
			mDatePickerField.setAccessible(true);
			Log.d("mytag", "mDatePickerField = " + mDatePickerField.getName());
			Log.d("mytag", "mDatePickerField.get(this)="
					+ mDatePickerField.get(this).getClass().toString());
			DatePicker mDatePicker = (DatePicker) mDatePickerField.get(this);
			Field mYearPickerField = mDatePicker.getClass().getDeclaredField(
					"mYearPicker");
			mYearPickerField.setAccessible(true);
			Object mYearPicker = mYearPickerField.get(mDatePicker);
			Log.d("mytag", "mYearPicker = " + mYearPicker.getClass().toString());
			Field mStartField = mYearPicker.getClass().getDeclaredField(
					"mStart");
			mStartField.setAccessible(true);
			mStartField.set(mYearPicker, DEFAULT_START_YEAR);
			Field mEndField = mYearPicker.getClass().getDeclaredField("mEnd");
			mEndField.setAccessible(true);
			mEndField.set(mYearPicker, DEFAULT_END_YEAR);
			// Method[] mm = mYearPicker.getClass().getDeclaredMethods();
			// for (Method o : mm) {
			// if ("setRange".equals(o.getName())) {
			// Log.d("mytag",
			// "m = " + o.getName() + " params1="
			// + o.getParameterTypes()[0] + " params2="
			// + o.getParameterTypes()[1]);
			// }
			// }
			// Method m = mYearPicker.getClass().getMethod("setRange",
			// new Class[] { int.class, int.class });
			// m.invoke(mYearPicker, DEFAULT_START_YEAR, DEFAULT_END_YEAR);//
			// 传给方法的的参数
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onDateChanged(DatePicker view, int year, int month, int day) {
		updateTitle(year, month, day);
	}

	public void updateTitle(int year, int month, int day) {
		Calendar mCalendar = Calendar.getInstance();
		mCalendar.set(Calendar.YEAR, year);
		mCalendar.set(Calendar.MONTH, month);
		mCalendar.set(Calendar.DAY_OF_MONTH, day);
		setTitle(DateFormat.format(FULL, mCalendar.getTime()));
	}

}
